package com.oracle.dao;

import com.oracle.dao.impl.UserDao;
import com.oracle.mapper.UserMapper;
import com.oracle.pojo.User;
import com.oracle.pojo.UserExample;
import com.oracle.pojo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.script.ScriptEngine;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> showUsers(UserVo userVo) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (userVo.getAdName()!=null && !"".equals(userVo.getAdName())){
            criteria.andAddressLike("%"+userVo.getAdName()+"%");
        }
        if (userVo.getUserName()!=null && !"".equals(userVo.getUserName())){
            criteria.andUsernameLike("%"+userVo.getUserName()+"%");
        }
        if (userVo.getRealName()!=null && !"".equals(userVo.getRealName())){
            criteria.andRealnameLike("%"+userVo.getRealName()+"%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        return users;
    }

    @Override
    public User SelectUserByUserName(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        List<User> users = userMapper.selectByExample(userExample);
        return user = users.isEmpty()?null:users.get(0);
    }

    @Override
    public String addNewUser(User user) {
        int i = userMapper.insertSelective(user);
        if (i>0){
            return "YES";
        }
        return "NO";
    }

    @Override
    public User findOneByUid(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public Integer updateUserInfo(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
